/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package forms;

import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.DataChangedListener;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.FlowLayout;
import entidade.AppHelper;
import entidade.Categoria;
import entidade.MobCatController;
import exceptions.ReturnWsException;
import form.components.CategoriaListRenderer;
import view.img.ImageStore;

/**
 *
 * @author ademar.victorino
 */
public class CategoriasRaizForm extends AbstractForm {

    private List list;
    private Label buscaLb = new Label();
    private TextField txBusca;
    private Image im = ImageStore.getInstance().getImage("busca");
    private Button btnSearchOk = new Button("Ok");
    private int lastSelectCidade = -1;

    public CategoriasRaizForm() {
        super("Categorias principais");

        buscaLb.setIcon(im);

        Container buscaContainer = new Container(new FlowLayout(Component.LEFT));

        buscaContainer.addComponent(buscaLb);
        buscaContainer.addComponent(getTxBusca());
        buscaContainer.addComponent(btnSearchOk);

        btnSearchOk.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                search(-1);

            }
        });

        Container listContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        if(lastSelectCidade == -1 || lastSelectCidade != AppHelper.getInstance().cidadeSelecionada){
            try {
                list = new List(MobCatController.getInstance().listCategoriaRaiz(AppHelper.getInstance().cidadeSelecionada));
                
            } catch (ReturnWsException ex) {
                ex.showError(InicialForm.getInstance());
            }
            lastSelectCidade = AppHelper.getInstance().cidadeSelecionada;
            listContainer.addComponent(list);
        }
        list.setListCellRenderer(new CategoriaListRenderer());

        list.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                
                WaitForm.getInstance().show();
                new Thread(new Runnable() {

                    public void run() {
                        listAction();
                    }
                }).start();

            }
        });

        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        addComponent(buscaContainer);
        addComponent(listContainer);

        addCommand(new Command("Voltar") {

            public void actionPerformed(ActionEvent evt) {

                InicialForm.getInstance().show();
            }
        });

        setFocused(list);
    }
    
    public void listAction(){
        new CategoriaForm().show((Categoria) list.getSelectedItem(), this);   
    }


    public TextField getTxBusca() {
        if (txBusca == null) {
            txBusca = new TextField();
            /*UsuarioConfig uc = AppHelper.getInstance().getUc();
            txBusca.setText(uc.getUltimaBusca() == null ? "" : uc.getUltimaBusca());*/
            int width = Display.getInstance().getDisplayWidth();
            int w = ((width - im.getWidth()) - (width * 30 / 100));
            txBusca.setPreferredW(w);
            txBusca.getStyle().setBgColor(0xffffff);
            txBusca.getStyle().setBgSelectionColor(0xff9a28);

            
            txBusca.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent arg0) {

                    search(arg0.getKeyEvent());

                }
            });
        }
        return txBusca;
    }

    private void search(int keyEvent) {
        System.out.println(keyEvent);
        if (keyEvent == -1) {

            String word = txBusca.getText();

            if (word.trim().length() == 1) {
                if (!Character.isDigit(word.charAt(0))) {
                    showMenuDialog(getWarningDialog());
                }
            } else {
                /* UsuarioConfig uc = AppHelper.getInstance().getUc();
                uc.setUltimaBusca(txBusca.getText());
                uc.save();*/
                setVisible(false);
            // new RegistrosForm().show(word);
            }
        }
    }

    private Dialog getWarningDialog() {

        final Dialog dlg = new Dialog("Seja mais específico em sua busca.");
        dlg.addCommand(new Command("Ok") {

            public void actionPerformed(ActionEvent arg0) {
                dlg.dispose();
            }
        });
        return dlg;
    }
}
